﻿@inject IStringLocalizer<Transfers> Localizer

<Transfer Items="@Items" @bind-Value="@SelectedValue" />
<div class="my-3">Value：@string.Join(";", SelectedValue.Select(i => i.Text))</div>
<Button Text="@Localizer["BindToWayButtonText1"]" OnClick="@OnAddItem" />

@code {
    [NotNull]
    private List<SelectedItem>? Items { get; set; }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // 模拟异步加载数据源
        await Task.Delay(100);

        Items = Enumerable.Range(1, 15).Select(i => new SelectedItem()
        {
            Text = $"{Localizer["Backup"]} {i:d2}",
            Value = i.ToString()
        }).ToList();

        var v = SelectedValue.ToList();
        v.AddRange(Items.Take(2));
        v.AddRange(Items.Skip(4).Take(1));
        SelectedValue = v;
    }

    private IEnumerable<SelectedItem> SelectedValue { get; set; } = Enumerable.Empty<SelectedItem>();

    private void OnAddItem()
    {
        var count = Items.Count + 1;
        Items.Add(new SelectedItem(count.ToString(), $"{Localizer["Backup"]} {count:d2}"));
    }
}
